/**
 * Copyright © 2016-2025 The Thingsboard Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.terracloud.server.dao.sql.log;

import com.terracloud.server.common.data.log.LogLevel;
import com.terracloud.server.common.data.log.LogType;
import com.terracloud.server.dao.model.sql.PlatformLogEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.UUID;

public interface PlatformLogRepository extends JpaRepository<PlatformLogEntity, UUID> {

    @Query("SELECT t FROM PlatformLogEntity t WHERE t.tenantId = :tenantId ORDER BY t.createdTime DESC")
    Page<PlatformLogEntity> findByTenantId(@Param("tenantId") UUID tenantId, Pageable pageable);

    @Query("SELECT t FROM PlatformLogEntity t WHERE t.tenantId = :tenantId AND t.userId = :userId ORDER BY t.createdTime DESC")
    Page<PlatformLogEntity> findByTenantIdAndUserId(@Param("tenantId") UUID tenantId, @Param("userId") UUID userId, Pageable pageable);

    @Query("SELECT t FROM PlatformLogEntity t WHERE t.tenantId = :tenantId AND t.logType = :logType ORDER BY t.createdTime DESC")
    Page<PlatformLogEntity> findByTenantIdAndType(@Param("tenantId") UUID tenantId, @Param("logType") LogType logType, Pageable pageable);

    @Query("SELECT t FROM PlatformLogEntity t WHERE t.tenantId = :tenantId AND t.logLevel = :logLevel ORDER BY t.createdTime DESC")
    Page<PlatformLogEntity> findByTenantIdAndLevel(@Param("tenantId") UUID tenantId, @Param("logLevel") LogLevel logLevel, Pageable pageable);

    @Query("SELECT t FROM PlatformLogEntity t WHERE t.tenantId = :tenantId AND t.userId = :userId AND t.logType = :logType ORDER BY t.createdTime DESC")
    Page<PlatformLogEntity> findByTenantIdAndUserIdAndType(@Param("tenantId") UUID tenantId, @Param("userId") UUID userId, @Param("logType") LogType logType, Pageable pageable);

    @Query("SELECT t FROM PlatformLogEntity t WHERE t.tenantId = :tenantId AND t.userId = :userId AND t.logLevel = :logLevel ORDER BY t.createdTime DESC")
    Page<PlatformLogEntity> findByTenantIdAndUserIdAndLevel(@Param("tenantId") UUID tenantId, @Param("userId") UUID userId, @Param("logLevel") LogLevel logLevel, Pageable pageable);

    @Query("SELECT t FROM PlatformLogEntity t WHERE t.tenantId = :tenantId AND t.businessId = :businessId ORDER BY t.createdTime DESC")
    Page<PlatformLogEntity> findByBusinessId(@Param("tenantId") UUID tenantId, @Param("businessId") String businessId, Pageable pageable);

    @Query("SELECT COUNT(t) FROM PlatformLogEntity t WHERE t.tenantId = :tenantId AND t.status = :status")
    long countByStatus(@Param("tenantId") UUID tenantId, @Param("status") String status);

    @Query("SELECT COUNT(t) FROM PlatformLogEntity t WHERE t.tenantId = :tenantId AND (t.logType = 'AUDIT_LOG' OR t.logType = 'SECURITY_LOG')")
    long countBySensitiveOperation(@Param("tenantId") UUID tenantId);
}